package com.cl.short_video_java.web;

import cn.hutool.core.map.MapUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.cl.short_video_java.common.Result;
import com.cl.short_video_java.mp.VideoProducer;
import com.cl.short_video_java.pojo.VideoUser;
import com.cl.short_video_java.service.VideoUserService;
import com.cl.short_video_java.service.VideoVideoService;
import com.cl.short_video_java.util.AliOssUtil;
import com.cl.short_video_java.util.IdAssignUtil;
import com.cl.short_video_java.util.IpUtils;
import com.fasterxml.jackson.core.JsonProcessingException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map;
import java.util.concurrent.Executor;
import java.util.concurrent.atomic.AtomicLong;
import java.util.concurrent.locks.ReentrantLock;

/**
 * @Author 付
 * @Date 2024-11-01 14:53
 * @Version 1.0
 * @description
 * @since 1.0
 */

@RestController
public class TestController {
	@Autowired
	AliOssUtil aliOssUtil;
	@Autowired
	VideoUserService videoUserService;
	@Autowired
	VideoVideoService videoService;
	@Autowired
	VideoProducer videoProducer;


	@Autowired
	private Executor asyncExecutor;

	@GetMapping("/test")
//	@Log(title = "测试", businessType = BusinessType.OTHER)
	public Result test(HttpServletRequest request) {
		String ipAddr = IpUtils.getIpAddr();
		String hostIp = IpUtils.getHostIp();
		String ipAddr1 = IpUtils.getIpAddr(request);
		String hostName = IpUtils.getHostName();
		Map<Object, Object> build = MapUtil.builder().put("ipAddr", ipAddr).put("hostIp", hostIp).put("hostName", hostName).put("ipAddr1", ipAddr1).build();
		/**
		 *  "hostName": "DESKTOP-62GKKUL",
		 *   "hostIp": "192.168.1.29",
		 *  "ipAddr1": "127.0.0.1",
		 *   "ipAddr": "127.0.0.1"
		 */
		return Result.success(build);
	}


	@GetMapping("/snock_id")
	public Result snockId(HttpServletRequest request) {
		String l = IdAssignUtil.nextId();
		videoUserService.list(new QueryWrapper<VideoUser>().select(VideoUser.class, item -> !item.getColumn().equals("password")));
		videoUserService.list(new QueryWrapper<VideoUser>().select("username", "address"));
		return Result.success(l);
	}

	@GetMapping("/sort")
	public Result sort() throws JsonProcessingException {
//		videoService.updateSort();
		videoService.getBank();
//		List<?> recommon = videoService.getRecommon(1);
		return Result.success();
	}

	@GetMapping("/send")
	public Result send() {
		Result result = new Result();
		result.setData("这是一条消息");
		videoProducer.sendJson(result);
		videoProducer.sendDelayUserMap(result);
		return result;
	}


	/**
	 * 线程池处理
	 *
	 * @param file
	 * @return
	 */
	@PostMapping("/upload")
	public Result uploadFile(@RequestParam("file") MultipartFile file) {
		asyncExecutor.execute(() -> {
			try {
				// 处理文件上传逻辑
				File dest = new File("path/to/save/" + file.getOriginalFilename());
				file.transferTo(dest);
			} catch (IOException e) {
				e.printStackTrace();
			}
		});
		return Result.success();
	}

	@GetMapping("/test1")
	public Result test1() {
		AtomicLong time = new AtomicLong(new Date().getTime());
		List list = new ArrayList();
		for (int i = 0; i < 1000000; i++) {
			list.add(i);
		}

		time.set(System.currentTimeMillis()-time.get());

		return Result.success(time);
	}

}
